﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sunimage.Spjg.BLL.Helper;
using Sunimage.Spjg.Common.SysMgr;
using Sunimage.Spjg.DAL.SysMgr;
using Sunimage.Spjg.Common.Exceptions;
using Sunimage.Spjg.Common;
using Sunimage.Spjg.DAL;
using System.Data.SqlClient;

namespace Sunimage.Spjg.BLL.SysMgr
{
    public class UserBl
    {
        public User Login(string username, string password)
        {
            User user = GetByUsername(username);
            if (user == null)
                return null;
            else
            {
                if (user.Password.Equals(password))
                    return user;
                else
                    return null;
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public List<User> GetUsers()
        {
            return UserDa.GetAll();
        }


        public void Delete(int id)
        {
            if (id < 1)
                throw new BusinessObjectException("要删除的用户不存在");
            User user = GetById(id);
            if (user == null)
                throw new BusinessObjectException("要删除的用户不存在");
            if (user.Username == "admin")
                throw new BusinessObjectException("不能删除admin用户");

            UserDa.Delete(id);
        }

        /// <summary>
        /// 添加工商局用户
        /// </summary>
        /// <param name="user"></param>
        public void CreateUser(User user)
        {
            if (GetByUsername(user.Username) != null)
                throw new BusinessObjectLogicException(string.Format("用户名{0}已经存在", user.Username));

            UserDa.Insert(user);
        }

        /// <summary>
        /// 修改工商局用户信息
        /// </summary>
        /// <param name="user"></param>
        public void UpdateUser(User user)
        {
            UserDa.Update(user);
        }
        /// <summary>
        /// 根据Id查找用户
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <returns>工商局用户实体类，不存在返回null</returns>
        public User GetById(int id)
        {
            User user = UserDa.GetAll().Find(u => u.Id.Equals(id));

            if (user == null)
                return null;
            else
                return user;
        }

        /// <summary>
        /// 根据用户名查找用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>工商局用户实体类，不存在返回null</returns>
        public User GetByUsername(string username)
        {
            return UserDa.GetAll().Find(u => u.Username.Equals(username));
        }

        /// <summary>
        /// 重置用户密码为默认密码
        /// </summary>
        /// <param name="id"></param>
        public void ResetPassword(int id)
        {
            User user = GetById(id);
            user.Password = SystemConfiguration.GetDefaultGsjUserPassword();
            UserDa.Update(user);
        }


    }
}
